package cn.wolfcode.luowowo2.website.web.controller;

import cn.wolfcode.luowowo2.article.domain.Destination;
import cn.wolfcode.luowowo2.article.domain.StrategyCommend;
import cn.wolfcode.luowowo2.article.domain.StrategyDetail;
import cn.wolfcode.luowowo2.article.domain.StrategyTag;
import cn.wolfcode.luowowo2.article.service.IDestinationService;
import cn.wolfcode.luowowo2.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo2.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo2.article.service.IStrategyTagService;
import cn.wolfcode.luowowo2.article.vo.StrategyStatisVO;
import cn.wolfcode.luowowo2.cache.domain.StrategyComment;
import cn.wolfcode.luowowo2.cache.prefix.StrategyStatisKeyPrefix;
import cn.wolfcode.luowowo2.cache.service.IStrategyCommentService;
import cn.wolfcode.luowowo2.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo2.common.query.StrategyCommendQueryObject;
import cn.wolfcode.luowowo2.common.query.StrategyDetailQueryObject;
import cn.wolfcode.luowowo2.common.util.AjaxResult;
import cn.wolfcode.luowowo2.member.domain.UserInfo;
import cn.wolfcode.luowowo2.search.query.SearchQueryObject;
import cn.wolfcode.luowowo2.search.service.IStrategySearchService;
import cn.wolfcode.luowowo2.search.template.StrategyTemplate;
import cn.wolfcode.luowowo2.search.vo.StatisVO;
import cn.wolfcode.luowowo2.website.web.annotation.RequireLogin;
import cn.wolfcode.luowowo2.website.web.annotation.UserParam;
import cn.wolfcode.luowowo2.common.query.StrategyCommentQueryObject;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Albert on 2019/7/10.
 */
@Controller
@RequestMapping("strategy")
public class StrategyController {
    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyTagService strategyTagService;

    @Reference
    private IStrategyCommentService strategyCommentService;

    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;

    @Reference
    private IStrategyCommendService strategyCommendService;

    @Reference
    private IStrategySearchService strategySearchService;

    @RequestMapping("detail")
    public String detail(Model model, Long id){
        StrategyDetail detail = strategyDetailService.get(id);
        model.addAttribute("detail",detail);
        StrategyStatisVO vo = strategyStatisRedisService.increaseView(StrategyStatisKeyPrefix.STRATEGY_STATIS_VO,id);
        model.addAttribute("vo",vo);
        //浏览后浏览数据加1
        strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_HOT,id,1);
        return "strategy/detail";
    }

    @RequestMapping("list")
    public String list(Model model, Long destId, @ModelAttribute("qo") StrategyDetailQueryObject qo){
        List<Destination> toastList = destinationService.getToastList(259L);
        model.addAttribute("toasts",toastList);
        Destination destination = destinationService.get(destId);
        model.addAttribute("dest",destination);

        List<StrategyTag> tags = strategyTagService.list();
        model.addAttribute("tags",tags);

        /*StrategyDetailQueryObject sdqo = new StrategyDetailQueryObject();
        sdqo.setCurrentPage(qo.getCurrentPage());
        sdqo.setPageSize(qo.getPageSize());*/
        PageInfo<StrategyDetail> pageInfo = strategyDetailService.query(qo);
        model.addAttribute("pageInfo",pageInfo);

        return "strategy/list";
    }

    @RequireLogin
    @RequestMapping("commentAdd")
    @ResponseBody
    public AjaxResult commentAdd(StrategyComment comment, @UserParam UserInfo userInfo){
        comment.setUserId(userInfo.getId());
        comment.setUsername(userInfo.getNickname());
        comment.setCity(userInfo.getCity());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setLevel(userInfo.getLevel());
        comment.setCreateTime(new Date());
        boolean flag = strategyCommentService.save(comment);
        StrategyStatisVO vo = strategyStatisRedisService.increaseReply(StrategyStatisKeyPrefix.STRATEGY_STATIS_VO, comment.getDetailId());
        AjaxResult ajaxResult = new AjaxResult(flag, "");
        ajaxResult.setData(vo.getReplynum());
        //评论后统计数据加1
        strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_HOT,comment.getDetailId(),1);
        return ajaxResult;
    }

    @RequestMapping("comment")
    public String comment(Model model, @ModelAttribute("qo")StrategyCommentQueryObject qo){
        Page<StrategyComment> page = strategyCommentService.query(qo);
        model.addAttribute("page",page);
        return "strategy/commentTpl";
    }

    @RequireLogin
    @RequestMapping("commentThumbUp")
    @ResponseBody
    public Boolean commentThumbUp(String toid,Long fromid){
        Boolean result = strategyCommentService.thumbUp(toid,fromid);
        return result;
    }

    //@RequireLogin
    @RequestMapping("favor")
    @ResponseBody
    public AjaxResult favor(Long sid,@UserParam UserInfo userInfo){
        if (userInfo==null) {
            return new AjaxResult("请先登录",AjaxResult.AJAX_UNLOGIN_CODE);
        }
        boolean flag = strategyStatisRedisService.favor(sid, userInfo.getId());
        AjaxResult ajaxResult = new AjaxResult(flag,"");
        StrategyStatisVO vo= strategyStatisRedisService.getFavorVO(sid);
        ajaxResult.addData(vo);

        //收藏后在推荐统计上加1
        if (flag) {
            strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_SORT,sid,1);
        }else{
            strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_SORT,sid,-1);
        }

        return ajaxResult;
    }

    //@RequireLogin
    @RequestMapping("strategyThumbup")
    @ResponseBody
    public AjaxResult strategyThumbup(Long sid ,@UserParam UserInfo userInfo){
        if (userInfo==null) {
            return new AjaxResult("请先登录",AjaxResult.AJAX_UNLOGIN_CODE);
        }
        boolean flag = strategyStatisRedisService.strategyThumbup(sid, userInfo.getId());
        AjaxResult ajaxResult = new AjaxResult(flag,"");
        StrategyStatisVO vo= strategyStatisRedisService.getFavorVO(sid);
        ajaxResult.addData(vo);

        //点赞后在统计上加1
        if (flag) {
            strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_SORT,sid,1);
        }
        return ajaxResult;
    }

    @RequestMapping("")
    public String index(Model model){
        List<StrategyStatisVO> list = strategyStatisRedisService.getAllStrategyCommends();
        ArrayList<StrategyStatisVO> abroadList = new ArrayList<>();
        ArrayList<StrategyStatisVO> innerList = new ArrayList<>();

        for (StrategyStatisVO vo : list) {
            if (vo.isIsabroad()) {
                if (abroadList.size()<10) {//海外攻略前10
                    abroadList.add(vo);
                }
            }else{
                if (innerList.size()<10) {//国内前10
                    innerList.add(vo);
                }
            }
        }

        model.addAttribute("abroadCds",abroadList);
        model.addAttribute("unabroadCds",innerList);

        //获取热门攻略
        List<StrategyStatisVO> hotList = strategyStatisRedisService.getHotStrategyCommends();
        List<StrategyStatisVO> topTenList = hotList.subList(0, 10);
        model.addAttribute("hotCds",topTenList);

        List<StrategyCommend> commends = strategyCommendService.getTop5Commends();
        model.addAttribute("commends",commends);


        //主题攻略推荐
        List<Map<String,Object>> themeCommends = strategySearchService.queryThemeCommends();
        model.addAttribute("themeCds",themeCommends);

        //导航栏
        //按省份分组
        List<StatisVO> inners = strategySearchService.querythemeCondition(SearchQueryObject.CONDITION_TYPE_INNER);
        model.addAttribute("chinas",inners);
        //国外按国家分组
        List<StatisVO> abroads = strategySearchService.querythemeCondition(SearchQueryObject.CONDITION_TYPE_ABROAD);
        model.addAttribute("abroads",abroads);
        //按主题分组
        List<StatisVO> themes = strategySearchService.querythemeCondition(SearchQueryObject.CONDITION_TYPE_THEME);
        model.addAttribute("themes",themes);

        return "strategy/search";
    }

    @RequestMapping("searchPage")
    public String searchPage(Model model,SearchQueryObject qo){
        Page<StrategyTemplate> page = strategySearchService.query(qo);
        model.addAttribute("page",page);
        return "strategy/searchPageTpl";
    }

}
